const Queue = require('async/queue');
const _ = require('lodash');
const RoomQueue = require('./roomQueue');
const SpiderBiliBili = require('./spider/SpiderBiliBili');
const SpiderCC = require('./spider/SpiderCC');
const SpiderChuShou = require('./spider/SpiderChuShou');
const SpiderEGame = require('./spider/SpiderEGame');
const SpiderLongZhu = require('./spider/SpiderLongZhu');
const SpiderHuoMao = require('./spider/SpiderHuoMao');
const SpiderHuYa = require('./spider/SpiderHuYa');
const SpiderPanda = require('./spider/SpiderPanda');
const SpiderQuanMin = require('./spider/SpiderQuanMin');
const SpiderZhanQi = require('./spider/SpiderZhanQi');
const SpiderDouYu = require('./spider/SpiderDouYu');
const SpiderKuaiShou = require('./spider/SpiderKuaiShou');

const spiders = {
  bilibili: SpiderBiliBili,
  cc: SpiderCC,
  chushou: SpiderChuShou,
  egame: SpiderEGame,
  longzhu: SpiderLongZhu,
  huomao: SpiderHuoMao,
  huya: SpiderHuYa,
  panda: SpiderPanda,
  quanmin: SpiderQuanMin,
  zhanqi: SpiderZhanQi,
  douyu: SpiderDouYu,
  kuaishou: SpiderKuaiShou,
};

/**
 * 爬虫队列
 * @param client
 * @param mysql
 * @param redis
 * @param status
 * @return []
 */
module.exports = ({client, mysql, redis, status}) => {
  const roomQueue = RoomQueue({client, mysql, redis, status});

  const queue = Queue((task, callback) => {
    (async () => {

      const {platform, type, data} = task;
      const spider = spiders[platform.tag];
      if (!spider)
        return;

      /** List 采集列表的直播间 */
      if (type === 'firstPage' || type === 'page') {
        try {
          const page = type === 'firstPage' ? spider.firstListPage() : data;
          const _data = await spider.getListPage(page);

          client.emit('page_data', {
            platform: task.platform,
            nextPage: _data.nextPage,
            hasNextPage: _data.hasNextPage,
            test: task.test,
          });

          _data.list.forEach(room => {

            room.platform = task.platform;

            //todo 明日之后 debug
            if (room.type && room.type.indexOf('明日之后') !== -1) {
              client.emit('data_debug', room)
            }

            roomQueue.push(room);
          });

        } catch (error) {
          task.error = JSON.stringify(task) + '\n======\n' + error.message +
            '\n' +
            error.stack;
          status.spider.error++;
          client.emit('retry', task);
        }
      }
      /** TAG 采集直播平台标签页面的直播间*/
      else if (type === 'firstTagPage' || type === 'tag') {
        try {
          const page = type === 'firstTagPage' ?
            spider.firstListPage() :
            data.page;
          const _data = await spider.getTagPage(page, data.tag);
          if (!_data)
            return;

          client.emit('tag_data', {
            platform: task.platform,
            nextPage: _data.nextPage,
            hasNextPage: _data.hasNextPage,
            test: task.test,
            type: data.type,
            tag: data.tag,
          });

          _data.list.forEach(room => {
            room.platform = task.platform;
            room.type = data.type;
            roomQueue.push(room);
          });
        } catch (error) {
          console.error(type, error);
          task.error = JSON.stringify(task) + '\n======\n' + error.message +
            '\n' +
            error.stack;
          status.spider.error++;
          client.emit('retry', task);
        }
      }
      //采集直播间信息
      else if (type === 'room') {
        try {
          await spider.getRoom(data);
          data.platform = task.platform;
          if (task.test)
            data.test = true;
          client.emit('room_data', data);
        } catch (error) {
          status.spider.error++;
        }
      }

      status.spider.finish++;

      callback();

    })();

  }, 2);

  queue.error = err => {
    status.spider.error++;
    console.error(err);
    client.emitError(err);
  };

  return [queue, roomQueue];
};